package ma.ensao.evalmetrics.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import ma.ensao.evalmetrics.model.MyFile;
import ma.ensao.evalmetrics.model.Result;
import ma.ensao.evalmetrics.model.ResultMethod;
import ma.ensao.evalmetrics.util.HibernateUtil;
/*
* R�cuperation des resultats de test d'un projet 
*/
public class ResultManager extends HibernateUtil {
	
	public void deleteResult(Result res) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(res);
		session.getTransaction().commit();
		
	}
	
	public Result add(Result result) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("Metriccccc");
		System.out.println(result.getAbs());
		session.save(result);
		session.getTransaction().commit();
		return result;
	}

	public List<Result> MesResultat(Long id) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		List<Result> res = (List<Result>) session.createQuery("from Result where idProjet=" + id + "").list();

		session.getTransaction().commit();

		return res;
	}
	
	public Result resultById(Long id) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Result res = (Result) session.createQuery("from Result where id = " + id ).uniqueResult();

		session.getTransaction().commit();

		return res;
	}
	
	public Result LastResult(Long id) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Result res = (Result) session.createQuery(
				"from Result where idProjet=" + id + " AND date IN (SELECT max(date) FROM Result where idProjet=" + id+")").uniqueResult();

		session.getTransaction().commit();

		return res;
	}
	
	public Result beforeLast(Long id) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Result res = (Result) session.createQuery(
				"from Result where idProjet="+id+" AND date IN (SELECT MAX( date ) FROM Result WHERE idProjet="+id+" AND date < ( SELECT MAX(date) FROM Result where idProjet="+id+"))").uniqueResult();

		session.getTransaction().commit();

		return res;
	}
	
	public int countTotal(Long id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		int count = ((Long)session.createQuery("select count(*) from Result where idProjet="+id).uniqueResult()).intValue();
		session.getTransaction().commit();
		return count;
	}
	
	@SuppressWarnings("unchecked")
	public List<Result> listAll(Long id,int startIndex, int pageSize,String jtSorting) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Result> results = null;
		try {
			Query q = session.createQuery("from Result where idProjet="+id+" ORDER BY "+jtSorting+" ");
			q.setFirstResult(startIndex);
			q.setMaxResults(pageSize);
			results = (List<Result>)q.list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return results;
	}
}